﻿using System;
using System.Linq;
using Microsoft.SharePoint.Taxonomy;

namespace SPContrib.SharePoint.Taxonomy.Extensions
{
    /// <summary>
    /// Extensions for the TaxonomySession class
    /// </summary>
    public static class TaxonomySessionExtensions
    {

        public static TermSet EnsureTermSet(this TaxonomySession instance, string termSetName, string termSetGroupName)
        {
            Group tsGroup = instance.EnsureTermSetGroup(termSetGroupName);
            TermSet ts = tsGroup.EnsureTermSet(termSetName);

            instance.DefaultKeywordsTermStore.CommitAll();

            return ts;
        }

        public static Group EnsureTermSetGroup(this TaxonomySession instance, string termSetGroupName)
        {
            Group tsGroup;
            bool match = instance.DefaultKeywordsTermStore
                            .FindTermSetGroupsByName(termSetGroupName)
                            .Execute(out tsGroup);

            if (match == false)
            {
                var TermStoresApartFromDefaultQuery = from termstore in instance.TermStores
                                                      where termstore.Id != instance.DefaultKeywordsTermStore.Id
                                                      select termstore;

                // try the other termstores (if any)
                foreach (var termstore in TermStoresApartFromDefaultQuery)
                {
                    match = termstore
                                .FindTermSetGroupsByName(termSetGroupName)
                                .Execute(out tsGroup);

                    if (match)
                    {
                        break; // we have a match
                    }
                }

                if (match == false)
                {
                    tsGroup = instance.DefaultKeywordsTermStore.CreateGroup(termSetGroupName);
                }
            }

            return tsGroup;
        }

        public static TermSetCollection FindTermSets(this TaxonomySession instance, string name)
        {
            TermStore ts = instance.DefaultKeywordsTermStore;
            
            // try the obvious way first
            TermSetCollection result = instance.FindTermSets(name, ts.DefaultLanguage);

            if (result.Count == 0)
            { 
                // no match, try the other languages
                var LanguagesApartFromDefaultQuery = from language in ts.Languages
                                                     where language != ts.DefaultLanguage
                                                     select language;

                foreach (var language in LanguagesApartFromDefaultQuery)
                {
                    result = instance.FindTermSets(name, language);
                    if (result.Count != 0)
                    {
                        break; // match
                    }
                }
            }
            return result;
                   
        }

        private static TermSetCollection FindTermSets(this TaxonomySession instance, string name, int lcid)
        {
            return instance.GetTermSets(name, lcid);
        }

    }
}
